package com.kye.express.business.waybill_take_photo;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.base.Logger;
import com.kye.express.R;
import com.kye.express.databinding.ItemWaybillTakePhotoImageBinding;
import com.kye.model.bean.waybill_take_photo.WaybillPhotograhbean;

import io.reactivex.functions.Consumer;

class WaybillTakePhotoImagetemViewBinder extends me.drakeet.multitype.ItemViewBinder<WaybillPhotograhbean, WaybillTakePhotoImagetemViewBinder.ViewHolder> {

    private Context mContext;
    private WaybillTakePhotoActivityContract.WaybillTakePhotoActivityView mWaybillTakePhotoActivityView;

    public WaybillTakePhotoImagetemViewBinder(Context context, WaybillTakePhotoActivityContract.WaybillTakePhotoActivityView waybillTakePhotoActivityView) {
        mContext = context;
        mWaybillTakePhotoActivityView = waybillTakePhotoActivityView;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_waybill_take_photo_image, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull WaybillPhotograhbean item) {
        holder.bindData(item);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private WaybillPhotograhbean data;
        ItemWaybillTakePhotoImageBinding dataBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            dataBinding = DataBindingUtil.bind(itemView);
        }


        public void bindData(WaybillPhotograhbean item) {
            data = item;
            if (data != null) {
                dataBinding.setData(data);
                if (getAdapter().getItemCount() < 9 && getPosition() == getAdapter().getItemCount() - 1) {
                    Glide.with(mContext)
                            .load(R.drawable.icon_add)
                            .into(dataBinding.ivPhoto);
                    dataBinding.ivDel.setVisibility(View.GONE);

                    RxView.clicks(dataBinding.ivPhoto)
                            .compose(new DebounceObservableTransformer<>())
                            .subscribe(new Consumer<Object>() {
                                @Override
                                public void accept(Object o) throws Exception {
                                    if (mWaybillTakePhotoActivityView != null) {
                                        mWaybillTakePhotoActivityView.onAddWallbillButtonClicked();
                                    }
                                }
                            });
                } else {
                    Glide.with(mContext)
                            .load(data.getImagePath())
                            .into(dataBinding.ivPhoto);
                    dataBinding.ivDel.setVisibility(View.VISIBLE);
                    RxView.clicks(dataBinding.ivPhoto)
                            .compose(new DebounceObservableTransformer<>())
                            .subscribe(new Consumer<Object>() {
                                @Override
                                public void accept(Object o) throws Exception {
                                    Logger.d("image icon");
                                }
                            });
                }
                RxView.clicks(dataBinding.ivDel)
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe(new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Exception {
                                getAdapter().getItems().remove(item);
                                getAdapter().notifyDataSetChanged();
                            }
                        });
            }
        }
    }
}
